package com.sqj.prototypePattern.deep;

import java.io.*;
import java.util.Date;

public class QitianDaSheng extends Monkey implements Cloneable, Serializable {
    public JinGuBang jinGuBang;

    public QitianDaSheng() {
        this.birthDay = new Date();
        this.jinGuBang = new JinGuBang();
    }

    @Override
    protected Object clone() {
        return this.deepClone();
    }

    public Object deepClone() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            QitianDaSheng copy = (QitianDaSheng) ois.readObject();
            copy.birthDay = new Date();
            return copy;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public QitianDaSheng shallowClone(QitianDaSheng target){
        QitianDaSheng qitianDaSheng=new QitianDaSheng();
        qitianDaSheng.height=target.height;
        qitianDaSheng.weight=target.weight;
        qitianDaSheng.jinGuBang=target.jinGuBang;
        qitianDaSheng.birthDay=new Date();
        return qitianDaSheng;
    }
}
